package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdcledtaddbzqxBaDetailBo;
import com.ruoyi.report_wjjhl.domain.vo.WdcledtaddbzqxBaDetailVo;
import com.ruoyi.report_wjjhl.domain.WdcledtaddbzqxBaDetail;
import com.ruoyi.report_wjjhl.mapper.WdcledtaddbzqxBaDetailMapper;
import com.ruoyi.report_wjjhl.service.IWdcledtaddbzqxBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 详细Service业务层处理
 *
 * @author zyb
 * @date 2024-08-05
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdcledtaddbzqxBaDetailServiceImpl implements IWdcledtaddbzqxBaDetailService {

    private final WdcledtaddbzqxBaDetailMapper baseMapper;

    /**
     * 查询详细
     */
    @Override
    public WdcledtaddbzqxBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询详细列表
     */
    @Override
    public TableDataInfo<WdcledtaddbzqxBaDetailVo> queryPageList(WdcledtaddbzqxBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdcledtaddbzqxBaDetail> lqw = buildQueryWrapper(bo);
        Page<WdcledtaddbzqxBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询详细列表
     */
    @Override
    public List<WdcledtaddbzqxBaDetailVo> queryList(WdcledtaddbzqxBaDetailBo bo) {
        LambdaQueryWrapper<WdcledtaddbzqxBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdcledtaddbzqxBaDetail> buildQueryWrapper(WdcledtaddbzqxBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdcledtaddbzqxBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WdcledtaddbzqxBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getQualId() != null, WdcledtaddbzqxBaDetail::getQualId, bo.getQualId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), WdcledtaddbzqxBaDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(bo.getNum1() != null, WdcledtaddbzqxBaDetail::getNum1, bo.getNum1());
        lqw.eq(bo.getNum2() != null, WdcledtaddbzqxBaDetail::getNum2, bo.getNum2());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), WdcledtaddbzqxBaDetail::getJiszb, bo.getJiszb());
        lqw.eq(bo.getJiancjg() != null, WdcledtaddbzqxBaDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), WdcledtaddbzqxBaDetail::getJiegpd, bo.getJiegpd());
        lqw.eq(bo.getShijbh() != null, WdcledtaddbzqxBaDetail::getShijbh, bo.getShijbh());
        lqw.eq(bo.getHuijl() != null, WdcledtaddbzqxBaDetail::getHuijl, bo.getHuijl());
        lqw.eq(bo.getEdtahl1() != null, WdcledtaddbzqxBaDetail::getEdtahl1, bo.getEdtahl1());
        lqw.eq(bo.getEdtahl2() != null, WdcledtaddbzqxBaDetail::getEdtahl2, bo.getEdtahl2());
        lqw.eq(bo.getEdtahlpjz() != null, WdcledtaddbzqxBaDetail::getEdtahlpjz, bo.getEdtahlpjz());
        lqw.eq(bo.getShiyzl() != null, WdcledtaddbzqxBaDetail::getShiyzl, bo.getShiyzl());
        return lqw;
    }

    /**
     * 新增详细
     */
    @Override
    public Boolean insertByBo(WdcledtaddbzqxBaDetailBo bo) {
        WdcledtaddbzqxBaDetail add = BeanUtil.toBean(bo, WdcledtaddbzqxBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改详细
     */
    @Override
    public Boolean updateByBo(WdcledtaddbzqxBaDetailBo bo) {
        WdcledtaddbzqxBaDetail update = BeanUtil.toBean(bo, WdcledtaddbzqxBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdcledtaddbzqxBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
